home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8388 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: new and delete with arrays
  5. Date: 17 Feb 1996 17:38:27 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4g53qj$42i@sparcserver.lrz-muenchen.de>
  9. References: <4g4loa$i1l@mordred.gatech.edu>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. Darin Heuermann <gt1792a@prism.gatech.edu> writes:
  13.  
  14. >When I allocate an array of classes with delete, should delete call all of 
  15. >the instances' destructors?  Here is an example of what I mean.
  16.  
  17. >//**********************************************
  18.  
  19. >class MYCLASS {
  20. >public:
  21. >   MYCLASS(void);
  22. >   ~MYCLASS(void);
  23. >} // MYCLASS
  24.  
  25. >MYCLASS *myClass;
  26.  
  27. >myClass = new MYCLASS[10];
  28. >delete myClass;
  29.  
  30. >//**********************************************
  31.  
  32. >I would expect all 10 instances to call ~MYCLASS() when delete myClass is 
  33. >called, but only the first instance's destructor is being called.  Is the 
  34. >a bug in the compiler or is this how it's supposed to work?
  35.  
  36. Neither the one nor the other, it's a bug in that piece of code. Try
  37.  
  38.   myClass = new MYCLASS[10];
  39.  
  40.   // Work with myClass, then ...
  41.  
  42.   delete [] myClass;
  43.  
  44. Kurt
  45. --
  46. | Kurt Watzka                             Phone : +49-89-2180-6254
  47. | watzka@stat.uni-muenchen.de
  48. | ua302aa@sunmail.lrz-muenchen.de
  49.  
  50.